#include "stdafx.h"
#include "PGLNet.h"
#include <vcclr.h> 
 
 
using namespace System;
using namespace System::Runtime::InteropServices;

#define FLOAT2PGL( var )\
float __pin * var ##_ptr = & var ##_ [0];\
CPGLVectorFPtr var(\
new CPGLVectorF(\
var ##_ptr , var ##_ ->Count\
));

namespace PGL
{
	private __gc class StringPtr
	{
	private:
		System::IntPtr m_ptr;

	public:
		StringPtr( System::String __gc* s){m_ptr = Marshal::StringToHGlobalAnsi( s ); };
		~StringPtr(){ Marshal::FreeHGlobal(m_ptr); };
		const char* ToCSTR()
		{
			return static_cast<const char*>( m_ptr.ToPointer() );
		}
	};
	

	public __gc class Engine
	{
	private:
		CPGLScript* m_script;
	public:
		Engine()
		{
			m_script = new CPGLScript();
		}
		~Engine()
		{
			delete m_script;
		}

		System::Int32 Figure()									{	return m_script->Figure();};
		System::Int32 Region()									{	return m_script->Region();};
		System::Int32 GetCR()									{	return m_script->GetCR();};
		System::Boolean SetCR( System::Int32 dwRegionID)		{	return m_script->SetCR( dwRegionID );};;
		System::Int32 GetCD() 									{	return m_script->GetCD();};
		System::Int32 DoModalRegion( 
			System::Int32 lRegionID, 
			System::Int32 dwFlags )								{	return m_script->DoModalRegion(lRegionID, dwFlags);};
		void CloseFigure( System::Int32 lFigureID)				{	m_script->CloseFigure(lFigureID);};
		void CloseAllFigures()									{	m_script->CloseAllFigures();};
		void CloseRegion( System::Int32 lRegionID )				{	m_script->CloseRegion(lRegionID);};
		void CloseAllRegions()									{	m_script->CloseAllRegions();};

		System::Int32 Divide(
			System::Int32 lRegionID, 
			System::UInt32 nRows, 
			System::UInt32 nCols)								{	return m_script->Divide(lRegionID,nRows,nCols);};
		
		System::Int32 Layout(
			System::Int32 lRegionID, 
			System::String __gc* szLayout)			
		{
			StringPtr* str=new StringPtr( szLayout );
			return m_script->Layout( lRegionID, str->ToCSTR() );
		};

		System::Int32 GetObject( System::Int32 lRegionID, System::UInt32 uIndex )
		{
			return m_script->GetObject(lRegionID,uIndex);
		};

		System::Int32 GetObjectsSize( System::Int32 lRegionID)
		{
			return m_script->GetObjectsSize(lRegionID);
		}

		void RemoveObject( System::Int32 lRegionID, System::UInt32 uIndex )
		{
			m_script->RemoveObject(lRegionID, uIndex);
		}

		System::Int32 GetChild( System::Int32 lRegionID, System::UInt32 uIndex )
		{
			return m_script->GetChild(lRegionID, uIndex);
		}

		System::Int32 GetChildrenSize( System::Int32 lRegionID )
		{
			return m_script->GetChildrenSize(lRegionID);
		}

		void RemoveChild( System::Int32 lRegionID, System::UInt32 uIndex )
		{
			m_script->RemoveChild(lRegionID, uIndex);
		}

		System::Int32 GetParent(System::Int32 uID )		{	return m_script->GetParent( uID );};
		System::Int32 OutOfChild()						{	return m_script->OutOfChild(); }
		System::Int32 Plot(System::Int32 lRegionID, float pX_  __gc[], float pY_ __gc[], System::String __gc* szFlags)
		{
			StringPtr* str=new StringPtr( szFlags );
			FLOAT2PGL( pX );
			FLOAT2PGL( pY );
			return m_script->Plot(
				lRegionID,
				pY,
				pY,
				str->ToCSTR()
				);
		}

		System::Int32 PlotY(System::Int32 lRegionID, float fX0, float fXStep, float pY_ __gc[], System::String __gc* szFlags)
		{
			StringPtr* str=new StringPtr( szFlags );
			FLOAT2PGL( pY );
			return m_script->PlotY(
				lRegionID,
				fX0,
				fXStep,
				pY,
				str->ToCSTR()
				);
		}
/*
		System::Int32 PlotLOD(System::Int32 lRegionID, CPGLDataPtr pX, CPGLDataPtr pY, float fCompressionRatio, System::String __gc* szFlags = _T(""));
	System::Int32 PlotLODY(System::Int32 lRegionID, float fX0, float fXStep, CPGLDataPtr pY, float fCompressionRatio, System::String __gc* szFlags = _T(""));
	System::Int32 PlotFcn(System::Int32 lRegionID, CPGLDataPtr pX, System::String __gc* szFcnX = _T("x"), System::String __gc* szFlags = _T(""));
	System::Int32 PlotFcnY(System::Int32 lRegionID, System::Int32 nSteps, float fX0, float fXStep, System::String __gc* szFcnX = _T("x"), System::String __gc* szFlags = _T(""));
	System::Int32 PlotHor(System::Int32 lRegionID, float fY, System::String __gc* szFlags = _T(""));
	System::Int32 PlotVer(System::Int32 lRegionID, float fX, System::String __gc* szFlags = _T(""));
	System::Int32 FixedStepMap(System::Int32 lRegionID, System::Int32 nXSteps, float fX0, float fXStep, System::Int32 nYSteps, float fY0, float fYStep, CPGLDataPtr pValues, System::String __gc* szFlags);
	System::Int32 RegularMap(System::Int32 lRegionID, CPGLDataPtr pX, CPGLDataPtr pY, CPGLDataPtr pValues, System::String __gc* szFlags);
	System::Int32 FixedStepVectorField(System::Int32 lRegionID, System::Int32 nXSteps, float fX0, float fXStep, System::Int32 nYSteps, float fY0, float fYStep, CPGLDataPtr pVx, CPGLDataPtr pVy, System::String __gc* szFlags);
	System::Int32 RegularVectorField(System::Int32 lRegionID, CPGLDataPtr pX, CPGLDataPtr pY, CPGLDataPtr pVx, CPGLDataPtr pVy, System::String __gc* szFlags);
	System::Int32 Histogram(System::Int32 lRegionID, CPGLDataPtr pY, System::String __gc* szFlags = _T(""));
	System::Int32 ErrorBar(System::Int32 lRegionID, CPGLDataPtr pX, CPGLDataPtr pY, CPGLDataPtr pErrorUp, CPGLDataPtr pErrorDown, System::String __gc* szFlags);
	System::Int32 ErrorBarY(System::Int32 lRegionID, float fX0, float fXStep, CPGLDataPtr pY, CPGLDataPtr pErrorUp, CPGLDataPtr pErrorDown, System::String __gc* szFlags);
	System::Int32 Scatter(System::Int32 lRegionID, CPGLDataPtr pX, CPGLDataPtr pY, CPGLDataPtr pValues, System::String __gc* szFlags);
	System::Int32 Stock(System::Int32 lRegionID, float fX0, float fXStep, CPGLDataPtr pOpen, CPGLDataPtr pHigh, CPGLDataPtr pLow, CPGLDataPtr pClose, System::String __gc* szFlags);
	System::Int32 CandleStick(System::Int32 lRegionID, float fX0, float fXStep, CPGLDataPtr pOpen, CPGLDataPtr pHigh, CPGLDataPtr pLow, CPGLDataPtr pClose, System::String __gc* szFlags);
	System::Int32 Image(System::Int32 lRegionID, System::String __gc* szFileName, System::String __gc* szFlags = _T(""));
	System::Int32 LeftLabel(System::Int32 lRegionID, System::String __gc* szLabel);
	System::Int32 BottomLabel(System::Int32 lRegionID, System::String __gc* szLabel);
	System::Int32 Title(System::Int32 lRegionID, System::String __gc* szLabel);
	System::Int32 RightLabel(System::Int32 lRegionID, System::String __gc* szLabel);
	System::Int32 Legend(System::Int32 lRegionID);
	System::Int32 Grid(System::Int32 lRegionID, System::String __gc* szState);
	void ZoomAll(System::Int32 lRegionID, System::Boolean bRecurse = true);
	void Zoom(System::Int32 lRegionID, CPGLRectD rect);
	System::Boolean GetZoom(System::Int32 lRegionID, CPGLRectD& rect);
	void AutoZoom(System::String __gc* szState );
	void Set(System::Int32 uObjectID, System::String __gc* szPropAndValues);
	void Get(System::Int32 uObjectID, System::String __gc* szProp, CString& sValue);
	void GetHelp( System::Int32 uID, CString& str);
	System::Int32 LoadFromXML(System::Int32 lRegionID, System::String __gc* szFileName );
	System::Boolean SaveToXML(System::Int32 lRegionID, System::String __gc* szFileName, System::Boolean bZipping= false );
	System::Boolean LoadCSVFile( System::Int32 lRegionID, System::String __gc* szFileName);	
	System::Boolean RenderToFile(System::Int32 lRegionID, System::String __gc* szFileName, System::String __gc* szFormat );
	System::Boolean RenderToStream(System::Int32 uRegion, IStream* pStream, System::String __gc* szFormat);
	System::Boolean RenderToImage(System::Int32 lRegionID, Gdiplus::Image* pImage);
	System::Boolean RenderToByteArray(System::Int32 lRegionID, SAFEARRAY* lpSafeArray, System::String __gc* szFormat);
*/
	};

};